/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import bean.DecimalFormattedField;
import controler.GrupoControler;
import controler.ProdutoControler;
import bean.Grupo;
import bean.Produto;
import java.util.List;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author Adriano
 */
public class JDProdutoView extends javax.swing.JDialog {

    /**
     * Creates new form JDProdutoView
     */
    private ProdutoControler produtoControler = new ProdutoControler();
    private Produto produto = new Produto();
    JFormattedTextField real = new DecimalFormattedField(DecimalFormattedField.REAL); 
    public JDProdutoView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        idGenerator();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTFCodigoProduto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTFCodigodeBarras = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTFReferencia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTFDescricao = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTFPreco = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTFCusto = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTFMargem = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTFUnidade = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jCmbGrupo = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jTFGramatura = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jCmbFornecedor = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jTFPeso = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTFObservacao = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jBSalvarProduto = new javax.swing.JButton();
        jBDeletarProduto = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jBConsultar = new javax.swing.JButton();
        jBFechar = new javax.swing.JButton();
        jBNovo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Codigo");

        jLabel2.setText("Codigo de Barras");

        jTFCodigodeBarras.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFCodigodeBarrasFocusLost(evt);
            }
        });

        jLabel3.setText("Referencia");

        jTFReferencia.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFReferenciaFocusLost(evt);
            }
        });

        jLabel4.setText("Descricao");

        jLabel5.setText("Preco");

        jTFPreco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFPrecoActionPerformed(evt);
            }
        });
        jTFPreco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFPrecoKeyReleased(evt);
            }
        });

        jLabel6.setText("Custo");

        jTFCusto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFCustoActionPerformed(evt);
            }
        });
        jTFCusto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFCustoKeyReleased(evt);
            }
        });

        jLabel7.setText("Margem");

        jTFMargem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFMargemActionPerformed(evt);
            }
        });
        jTFMargem.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFMargemKeyReleased(evt);
            }
        });

        jLabel8.setText("Unidade");

        jLabel9.setText("Grupo");

        carregaGrupo();
        jCmbGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCmbGrupoActionPerformed(evt);
            }
        });

        jLabel10.setText("Gramatura");

        jLabel11.setText("Fornecedor");

        jLabel12.setText("Peso");

        jTFPeso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFPesoActionPerformed(evt);
            }
        });
        jTFPeso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFPesoKeyReleased(evt);
            }
        });

        jLabel13.setText("Observação");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTFCodigoProduto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(33, 33, 33)
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTFCodigodeBarras, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTFReferencia)))
                .addGap(10, 10, 10))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTFDescricao)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabel5)
                                            .addGap(48, 48, 48)
                                            .addComponent(jLabel6))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jTFPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jTFCusto, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel7)
                                        .addComponent(jTFMargem, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addComponent(jCmbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jCmbFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTFUnidade)
                            .addComponent(jTFGramatura)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel10))
                                .addGap(0, 73, Short.MAX_VALUE))
                            .addComponent(jTFPeso))))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jLabel13)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTFObservacao)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFCodigoProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFCodigodeBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTFDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFCusto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFMargem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCmbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFGramatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCmbFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTFObservacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jBSalvarProduto.setText("Salvar");
        jBSalvarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalvarProdutoActionPerformed(evt);
            }
        });

        jBDeletarProduto.setText("Deletar");
        jBDeletarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBDeletarProdutoActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jBConsultar.setText("Consultar");
        jBConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBConsultarActionPerformed(evt);
            }
        });

        jBFechar.setText("Fechar");
        jBFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jBSalvarProduto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBDeletarProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBConsultar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBFechar)
                .addGap(0, 22, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSalvarProduto)
                    .addComponent(jBDeletarProduto)
                    .addComponent(jBCancelar)
                    .addComponent(jBConsultar)
                    .addComponent(jBFechar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBNovo.setText("Novo");
        jBNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBNovo)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBNovo)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBSalvarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalvarProdutoActionPerformed
        salvarProduto();
    }//GEN-LAST:event_jBSalvarProdutoActionPerformed

    private void jBDeletarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBDeletarProdutoActionPerformed
        deletarProduto();
    }//GEN-LAST:event_jBDeletarProdutoActionPerformed

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        cancelar();
    }//GEN-LAST:event_jBCancelarActionPerformed

    private void jBFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFecharActionPerformed
        dispose();
    }//GEN-LAST:event_jBFecharActionPerformed

    private void jBConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBConsultarActionPerformed
        ConsultarProduto();
    }//GEN-LAST:event_jBConsultarActionPerformed

    private void jCmbGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCmbGrupoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCmbGrupoActionPerformed

    private void jTFPrecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFPrecoActionPerformed
       double d = (double) real.getValue();
    }//GEN-LAST:event_jTFPrecoActionPerformed

    private void jTFCustoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFCustoActionPerformed
        double d = (double) real.getValue();
    }//GEN-LAST:event_jTFCustoActionPerformed

    private void jTFMargemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFMargemActionPerformed
         double d = (double) real.getValue();
    }//GEN-LAST:event_jTFMargemActionPerformed

    private void jTFPesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFPesoActionPerformed
         double d = (double) real.getValue();
    }//GEN-LAST:event_jTFPesoActionPerformed

    private void jTFCodigodeBarrasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFCodigodeBarrasFocusLost
         if (jTFCodigodeBarras.getText().trim().equals("") || jTFCodigodeBarras.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o campo Codigo de Barras!");
            jTFCodigodeBarras.requestFocus();
        }
    }//GEN-LAST:event_jTFCodigodeBarrasFocusLost

    private void jTFReferenciaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFReferenciaFocusLost
       if (jTFReferencia.getText().trim().equals("") || jTFReferencia.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o campo Referencia!");
            jTFReferencia.requestFocus();
        }
    }//GEN-LAST:event_jTFReferenciaFocusLost

    private void jTFPrecoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFPrecoKeyReleased
        jTFPreco.setText(jTFPreco.getText().replaceAll("[^0-9]", ""));
    }//GEN-LAST:event_jTFPrecoKeyReleased

    private void jTFCustoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFCustoKeyReleased
        jTFCusto.setText(jTFCusto.getText().replaceAll("[^0-9]", ""));
    }//GEN-LAST:event_jTFCustoKeyReleased

    private void jTFMargemKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFMargemKeyReleased
        jTFMargem.setText(jTFMargem.getText().replaceAll("[^0-9]", ""));
    }//GEN-LAST:event_jTFMargemKeyReleased

    private void jTFPesoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFPesoKeyReleased
       jTFPeso.setText(jTFPeso.getText().replaceAll("[^0-9]", "")); 
    }//GEN-LAST:event_jTFPesoKeyReleased

    private void jBNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoActionPerformed
       produto = produtoControler.criarProduto();
       buscarId();
       
    }//GEN-LAST:event_jBNovoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDProdutoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDProdutoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDProdutoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDProdutoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JDProdutoView dialog = new JDProdutoView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBConsultar;
    private javax.swing.JButton jBDeletarProduto;
    private javax.swing.JButton jBFechar;
    private javax.swing.JButton jBNovo;
    private javax.swing.JButton jBSalvarProduto;
    private javax.swing.JComboBox jCmbFornecedor;
    private javax.swing.JComboBox jCmbGrupo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTFCodigoProduto;
    private javax.swing.JTextField jTFCodigodeBarras;
    private javax.swing.JTextField jTFCusto;
    private javax.swing.JTextField jTFDescricao;
    private javax.swing.JTextField jTFGramatura;
    private javax.swing.JTextField jTFMargem;
    private javax.swing.JTextField jTFObservacao;
    private javax.swing.JTextField jTFPeso;
    private javax.swing.JTextField jTFPreco;
    private javax.swing.JTextField jTFReferencia;
    private javax.swing.JTextField jTFUnidade;
    // End of variables declaration//GEN-END:variables
    private Produto getDadosTelaProduto() {
        Produto p = new Produto();
        p.setProCod(Integer.parseInt(jTFCodigoProduto.getText()));
        p.setProBar(jTFCodigodeBarras.getText());
        p.setProRef(jTFReferencia.getText());
        p.setProDes(jTFDescricao.getText());
        p.setProPre(Double.parseDouble(jTFPreco.getText()));
        p.setProCus(Double.parseDouble(jTFCusto.getText()));
        p.setProMar(Double.parseDouble(jTFMargem.getText()));
        p.setProUni(jTFUnidade.getText());
        p.setProGra(jTFGramatura.getText());
        //p.setFornecedor(Integer.parseInt(jTFFornecedor.getText()));
        p.setProPes(Double.parseDouble(jTFPeso.getText()));
        p.setProObs(jTFObservacao.getText());
        Grupo grupo = new Grupo();
        grupo.setGruCod(jCmbGrupo.getSelectedIndex() + 1);
        p.setGrupo(grupo);
        return p;
    }

    private void setDadosTelaProduto(Produto p) {
        jTFCodigoProduto.setText(String.valueOf(p.getProCod()));
        jTFCodigodeBarras.setText(String.valueOf(p.getProBar()));
        jTFReferencia.setText(p.getProRef());
        jTFDescricao.setText(p.getProDes());
        jTFPreco.setText(String.valueOf(p.getProPre()));
        jTFCusto.setText(String.valueOf(p.getProCus()));
        jTFMargem.setText(String.valueOf(p.getProMar()));
        jTFUnidade.setText(String.valueOf(p.getProUni()));
        jTFGramatura.setText(p.getProGra());
        //jTFFornecedor.setText(String.valueOf(p.getFornecedor()));
        jTFPeso.setText(String.valueOf(p.getProPes()));
        jTFObservacao.setText(p.getProObs());
        jCmbGrupo.setSelectedItem(p.getGrupo());
    }

    private void salvarProduto() {
        Produto pro = getDadosTelaProduto();
        produtoControler.Salvar(pro);
        JOptionPane.showMessageDialog(null, "Cadastro Salvo Com Sucesso", "Produtos", JOptionPane.INFORMATION_MESSAGE);
        // Limpar os Campos e Gerar um novo ID
        cancelar();

    }

    private void deletarProduto() {
        int id = Integer.parseInt(jTFCodigoProduto.getText());
        Produto p = new ProdutoControler().BuscarProduto(id);
        produtoControler.Deletar(p);
        JOptionPane.showMessageDialog(null, "Cadastro excluido Com Sucesso", "Produtos", JOptionPane.INFORMATION_MESSAGE);
    }
   
    private void cancelar() {
        jTFCodigoProduto.setText("");
        jTFReferencia.setText("");
        jTFCodigodeBarras.setText("");
        jTFDescricao.setText("");
        jTFPreco.setText("");
        jTFCusto.setText("");
        jTFMargem.setText("");
        jTFUnidade.setText("");
        jTFGramatura.setText("");
        //jTFFornecedor.setText("");
        jTFPeso.setText("");
        jTFObservacao.setText("");
    }

    private void ConsultarProduto() {
        JDConsultarProduto cp
                = new JDConsultarProduto(null, true);
        cp.setVisible(true);
        Produto p = cp.getProduto();
        if (p != null) {
            setDadosTelaProduto(p);
        } else {
            cancelar();
        }
    }

    private void Fechar() {
        System.exit(0);
    }

    private void carregaGrupo() {
        GrupoControler grupoControler = new GrupoControler();
        List<Grupo> list = grupoControler.BuscarTodos();
        jCmbGrupo.removeAllItems();
        for (Grupo gro : list) {
            jCmbGrupo.addItem(gro.getGruNom());
        }
    }
    private void carregaFornecedor() {
        GrupoControler grupoControler = new GrupoControler();
        List<Grupo> list = grupoControler.BuscarTodos();
        jCmbFornecedor.removeAllItems();
        for (Grupo gro : list) {
            jCmbFornecedor.addItem(gro.getGruNom());
        }
    }

    private void idGenerator() {
        jTFCodigoProduto.setEnabled(false);
        int i = 1;
        Session session = HibernateUtil.getSession();
        List<Produto> lista = session.createCriteria(Produto.class
                ).list();// Irá fazer uma Lista
        for (Produto produto : lista) {
            i = produto.getProCod()+ 1;

        }
        jTFCodigoProduto.setText(Integer.toString(i));
    }
    private void buscarId(){
        List <Produto> list = produtoControler.BuscarTodosProdutos();
        int maiorID = 0;
        for (Produto p : list){
            if(p.getProCod() > maiorID){
                maiorID = p.getProCod();
            }
        }
        jTFCodigoProduto.setText(String.valueOf(maiorID+1));
    }

}
